package com.wulang.extension.controller;


import com.wulang.extension.constants.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/producer")
public class ProducerController {

    //Autowired 如果用户没有自创建 spring就帮创建 如果自己创建了 那么就不会帮助创建了
    @Resource(name = "rabbitTemplate")
    RabbitTemplate rabbitTemplate;
    @Resource(name = "confirmRabbitTemplate")
    RabbitTemplate confirmRabbitTemplate;
    @Resource(name = "returnRabbitTemplate")
    RabbitTemplate returnRabbitTemplate;

    //消息确认机制测试
    @RequestMapping("/ack")
    public String producer() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE, "ack", "comsumer ack mode test");
        return "OK";
    }

    //消息持久化测试
    @RequestMapping("pers")
    public String pers() {
        Message message = new Message("Persistence test....".getBytes());
        //发送持久化消息
//        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        //非持久化的消息
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PERS_EXCHANGE, "pers", message);
        return "OK";
    }

    /**
     * 每个RabbitTmplate只能设置一次setConfirmCallback
     * 其次对于其他所有接口内的rbtmlate也被设置了这个回调函数
     */
    //消息确认
//    @RequestMapping("confirm")
//    public String confirm() {
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                System.out.println("执行了confirm方法");
//                if(ack) {
//                    System.out.printf("收到消息, 消息ID: %s, cause: %s",correlationData.getId(), cause);
//                } else {
//                    System.out.printf("未收到消息, 消息ID: %s, cause: %s", correlationData.getId(), cause);
//                }
//            }
//        });
//        CorrelationData correlationData = new CorrelationData("1");
//        rabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE + 1, "confirm", "confirm test..", correlationData);
//        return "ok";
//    }
    @RequestMapping("confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE, "confirm111", "confirm test....", correlationData);
        return "ok";
    }

    @RequestMapping("return")
    public String returnM() {
        CorrelationData correlationData = new CorrelationData("2");
        returnRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE, "confirm111", "return test....", correlationData);
        return "OK";
    }

    @RequestMapping("retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "retry test....");
        return "ok";
    }

    //    @RequestMapping("ttl")
//    public String ttl() {
//        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                //单位毫秒
//                message.getMessageProperties().setExpiration("10000");//10s
//                return message;
//            }
//        };
//        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test ... ", messagePostProcessor);
//        return "ok";
//    }
    @RequestMapping("ttl")
    public String ttl() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test ... ", message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        return "ok";
    }

    @RequestMapping("ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test ... ", message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        return "ok";
    }

    @RequestMapping("dlx")
    public String dxl() {
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "test dlx...");
        return "ok";
    }

//    @RequestMapping("delay")
//    public String dxl2() {
//        //10s后消费
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "test dlx..." + new Date(), message -> {
//            message.getMessageProperties().setExpiration("10000");
//            return message;
//        });
//        //20s后消费
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "test dlx..." + new Date(), message -> {
//            message.getMessageProperties().setExpiration("20000");
//            return message;
//        });
//        return "ok";
//    }


    @RequestMapping("delay")
    public String delay() {
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test...." + new Date(), messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setDelayLong(10000L);
            return messagePostProcessor;
        });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test...." + new Date(), messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setDelayLong(20000L);
            return messagePostProcessor;
        });
        return "ok";
    }


}
